package com.starrin.tennisfinder.scheduler;

import org.apache.log4j.Logger;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.concurrent.*;

/**
 * Created by larsstarrin on 02/02/14.
 */
public class StartSchedulerListener implements ServletContextListener {

    private static final Logger logger = Logger.getLogger(StartSchedulerListener.class);

    private ScheduledExecutorService service;

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        logger.info("ServletContext destroyed, stopping scheduler");
        service.shutdownNow();
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        refreshData();
    }

    public void refreshData() {
        service = Executors.newScheduledThreadPool(4);

        RefreshTaskCommodus refreshAvailabilityTaskHellas = new RefreshTaskCommodus("hellas", "19", "1|2|3", "4|5|6");
        RefreshTaskCommodus refreshAvailabilityTaskKarrtorp = new RefreshTaskCommodus("karrtorp", "9", "1|2|3|4", null);
        RefreshTaskMyCourt refreshAvailabilitySolna = new RefreshTaskMyCourt("solna", "388", "7599|7600|7601|7602|7603|7604|7605", null);
        RefreshTaskCommodus refreshAvailabilityTaskFarsta = new RefreshTaskCommodus("farsta", "8", "1|2|3|4|5|6", null);
        RefreshTaskMyCourt refreshAvailabilityTaskLunkan = new RefreshTaskMyCourt("stockholmtennis", "333", "5112|5113|5114|5115|5116|5117|5118", null);
        RefreshTaskMyCourt refreshMalmen = new RefreshTaskMyCourt("malmen", "386", "5805|5806|5807|5808|5808", "5809|5810");
        RefreshTaskMyCourt refreshTyreso = new RefreshTaskMyCourt("tyreso", "364", "5584|5585|5586|5587|5588", null);
        RefreshTaskMyCourt refreshSumpan = new RefreshTaskMyCourt("sumpan", "332", "5200|5201|5202|5203", "5204|5205");
        RefreshTaskSalk refreshTaskSalk = new RefreshTaskSalk("salk");
        RefreshTennisStadion refreshTennisStadion = new RefreshTennisStadion("tennisstadion");
        //RefreshBokaTennis refreshBokaTennis = new RefreshBokaTennis("smedslatten");

        service.scheduleAtFixedRate(refreshAvailabilityTaskHellas, 10, 240, TimeUnit.SECONDS);
        service.scheduleAtFixedRate(refreshAvailabilityTaskKarrtorp, 30, 240, TimeUnit.SECONDS);
        service.scheduleAtFixedRate(refreshAvailabilitySolna, 50, 240, TimeUnit.SECONDS);
        service.scheduleAtFixedRate(refreshAvailabilityTaskFarsta, 70, 240, TimeUnit.SECONDS);
        service.scheduleAtFixedRate(refreshAvailabilityTaskLunkan, 90, 240, TimeUnit.SECONDS);
        service.scheduleAtFixedRate(refreshSumpan, 110, 240, TimeUnit.SECONDS);
        service.scheduleAtFixedRate(refreshTaskSalk, 130, 240, TimeUnit.SECONDS);
        service.scheduleAtFixedRate(refreshTennisStadion, 150, 240, TimeUnit.SECONDS);
        service.scheduleAtFixedRate(refreshMalmen, 170, 240, TimeUnit.SECONDS);
        service.scheduleAtFixedRate(refreshTyreso, 190, 240, TimeUnit.SECONDS);
        //service.scheduleAtFixedRate(refreshBokaTennis, 170, 240, TimeUnit.SECONDS);   //smedslätten
    }
}
